package com.ling;

import com.ling.pl.core.commons.springmvc.ResponseBodyWrapFactoryBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.ImportResource;
import org.springframework.core.convert.converter.Converter;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by bo.wang on 2016/8/6.
 * 定义spring bean Scan范围
 * 提供对老配置的兼容 对xml类型spring 定义的文件位置进行规范
 * 负责注册除Controller等web层以外的所有bean，包括aop代理，service层，dao层，缓存，等等
 */
@Configuration
//@ComponentScan(excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, value = {Controller.class})})
//@EnableJpaRepositories(repositoryBaseClass = BaseDao.class)
@EnableAspectJAutoProxy(proxyTargetClass = true)//激活自动代理功能 即 @Aspect 有效
@ImportResource(locations = "classpath*:beans/**/*.xml")//提供对老配置的兼容
public class SpringConfig {
    /*
     *
     * <!-- 激活自动代理功能 即 @Aspect 有效 -->
     * <aop:aspectj-autoproxy proxy-target-class="true" />
     *
     * @EnableAspectJAutoProxy(proxyTargetClass=true) 与声明下面的bean作用相同
     */
//	@Bean
//	public AnnotationAwareAspectJAutoProxyCreator annotationAwareAspectJAutoProxyCreator() {
//		logger.info("AnnotationAwareAspectJAutoProxyCreator");
//		AnnotationAwareAspectJAutoProxyCreator aspectJAutoProxyCreator = new AnnotationAwareAspectJAutoProxyCreator();
//		// false:使用JDK动态代理织入增强 [基于目标类的接口] true:使用CGLib动态代理织入增强[基于目标类]
//		aspectJAutoProxyCreator.setProxyTargetClass(true);
//		return aspectJAutoProxyCreator;
//	}
    @Bean
    @ConditionalOnProperty(name = "ling.ResponseBodyWrap.enabled", havingValue = "true")
    public ResponseBodyWrapFactoryBean getResponseBodyWrap() {
        return new ResponseBodyWrapFactoryBean();
    }

    @Bean
    public Converter<String, Date> addNewConvert() {
        return new Converter<String, Date>() {
            private SimpleDateFormat datetimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

            @Override
            public Date convert(String source) {
                if (!StringUtils.hasLength(source)) {
                    //①如果source为空 返回null
                    return null;
                }
                if (StringUtils.hasText(source)) {
                    try {
                        if (source.indexOf(":") == -1 && source.length() == 10) {
                            return this.dateFormat.parse(source);
                        } else if (source.indexOf(":") > 0 && source.length() == 19) {
                            return this.datetimeFormat.parse(source);
                        } else if (source.length() == 13) {
                            return new Date(Long.parseLong(source));
                        } else {
                            throw new IllegalArgumentException("Could not parse date, date format is error ");
                        }
                    } catch (ParseException ex) {
                        IllegalArgumentException iae = new IllegalArgumentException("Could not parse date: " + ex.getMessage());
                        iae.initCause(ex);
                        throw iae;
                    }
                } else {
                    return null;
                }
            }
        };
    }
}
